import { FakeApi } from '@wowon/utils';
import { getCurrentUser } from '../../auth-center';

const initData = [
  {
    scope: 'web',
    label: '系统副标题',
    key: 'SITE_TITLE_SUB',
    enable: '1',
    value: '欢迎回来~',
    description: '系统副标题，显示在登录页',
  },
  {
    label: '地图默认缩放等级',
    key: 'MAP_ZOOM',
    enable: '1',
    scope: 'web',
    value: '12',
    description: '地图默认缩放等级',
  },
  {
    label: '地图默认中心点',
    key: 'MAP_CENTER_POSITION',
    enable: '1',
    scope: 'web',
    value: '120.318757,31.497554',
    description: '地图默认中心点',
  },
  {
    label: '地图AK',
    key: 'MAP_AK',
    enable: '1',
    scope: 'web',
    value: '',
    description: '系统地图ak',
  },
  {
    label: '系统问候语模板',
    key: 'TEMPLATE_WELCOME',
    enable: '1',
    scope: 'web',
    value: 'Hi, ${username}！欢迎使用 ${title}！',
    description: '系统工作台页面问候语模板',
  },
  {
    label: '系统图标路径',
    key: 'ICON_SCRIPT_URL',
    enable: '1',
    scope: 'web',
    value: '//at.alicdn.com/t/c/font_3310509_corhvjpk8ec.js',
    description: '系统图标文件的地址',
  },
  {
    label: '默认主题',
    key: 'Theme',
    enable: '1',
    scope: 'web',
    value: 'default-dark',
    description: '系统默认的主题',
  },
  {
    key: 'COPYRIGHT',
    label: '版权声明',
    value: 'wowon Admin © {year}',
    description: '系统版权声明',
    scope: 'web',
    enable: '1',
  },
  {
    key: 'SITE_TITLE',
    label: '系统标题',
    value: 'wowon Admin(Mock)',
    description: '系统主标题',
    scope: 'web',
    enable: '1',
  },
  {
    key: 'SITE_LOGO',
    label: '系统Logo',
    value: '/wowon/logo.png',
    description: '系统Logo',
    scope: 'web,h5,android,apple,harmony',
    enable: '1',
  },
];

export const paramList = new FakeApi(initData, {
  queryType: {
    key: 'like',
    label: 'like',
    value: 'like',
    scope: 'like',
    enable: 'is',
  },
  sort: () => 0,
});

export default {
  'GET /mock/param/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await paramList.query(req.query);
    res.json(result);
  },
  'POST /mock/param/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await paramList.create(req.body);
    res.json(result);
  },
  'POST /mock/param/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await paramList.update(req.body);
    res.json(result);
  },
  'POST /mock/param/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await paramList.remove(req.body);
    res.json(result);
  },
  'POST /mock/param/update/enable': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const { id, enable } = req.body;
    const result = await paramList.update({ id, enable });
    res.json(result);
  },
};
